package cn.beyond.vehicleManagement.controller;

import cn.beyond.vehicleManagement.pojo.SysMenu;
import cn.beyond.vehicleManagement.service.MenuService;
import cn.beyond.vehicleManagement.util.PageUtil;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet(urlPatterns = {"/menuServlet"})
public class MenuServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //定义全局变量
        resp.setContentType("application/json;charset=utf-8");
        resp.setCharacterEncoding("utf-8");
        MenuService service = new MenuService();
        String opr = req.getParameter("opr");
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        SysMenu sysMenu = new SysMenu();
        Map<String, Object> map = new HashMap<>();
        PrintWriter out = resp.getWriter();
        List<SysMenu> sysMenuList = new ArrayList<>();
        System.out.println(opr);
        switch (opr) {
//            case "addvehicleProcurement":
//                boolean addflag= addUser(req,userService);
//                out.print(addflag);
//                break;
            //删除菜单
            case "deleteMenu":
                int delId = Integer.parseInt(req.getParameter("Id"));
                boolean delflag = false;
                int delcount = service.deleteMenu(delId, "id");
                System.out.println(delcount);
                if (delcount > 0) {
                    delflag = true;
                }
                out.print(delflag);
                break;
                case "updateMenu":

               break;
            case "selectMenu":
                String menu = req.getParameter("menu");
                map.put("name", menu);
                sysMenu = service.selectMenuByOne(sysMenu, map);
                String Menu=JSON.toJSONString(sysMenu);
                out.print(Menu);
                map.clear();
                break;
            case "selectMenuTwo":
                int id= Integer.parseInt(req.getParameter("Id"));
                System.out.println(id);
                map.put("parent_id",id);
                sysMenuList=service.selectUserByCondition(sysMenu,map);
                for(SysMenu sysMenu1:sysMenuList){
                    System.out.println(sysMenu1);
                }
                String MenuTwo=JSON.toJSONString(sysMenuList);
                out.print(MenuTwo);
                map.clear();
                break;
            case "listMenu":
                //列举出角色列表
//                sysMenuList = service.selectUserByCondition(sysMenu, map);
                int startRow= Integer.parseInt(req.getParameter("startRow"));
                int pageSize= Integer.parseInt(req.getParameter("pageSize"));
                sysMenuList= PageUtil.getPageUtil(startRow,pageSize,"sys_menu",sysMenu);
                int totals=PageUtil.getPageCount("sys_menu");
                int totalpage=totals%pageSize==0?(totals/pageSize):(totals/pageSize+1);
                List<Integer>totalinfo=new ArrayList<>();
                totalinfo.add(totals);
                totalinfo.add(totalpage);
                List<Object>listListMap=new ArrayList<>();
                listListMap.add(totalinfo);
                listListMap.add(sysMenuList);
                String listJson=JSON.toJSONString(listListMap);
                out.print(listJson);
                break;
                //列出一级菜单
            case "listMenuOne":
                map.put("parent_id",0);
                sysMenuList=service.selectUserByCondition(sysMenu,map);
                String menuOnelist=JSON.toJSONString(sysMenuList);
                out.print(menuOnelist);
                map.clear();
                break;

        }
    }


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
